//
//  CQHomeTableViewCell.swift
//  swift-weibo
//
//  Created by 陈乾 on 15/10/31.
//  Copyright © 2015年 陈乾. All rights reserved.
//

import UIKit




class CQHomeTableViewCell: UITableViewCell {
  
    
    
    //MARK: - 属性
    //常量
    let CQMargin:CGFloat = 8
    
     ///图片视图 宽度/高度约束
    var pictureConsWidth: NSLayoutConstraint?
    var pictureConsHeight: NSLayoutConstraint?

    
     ///模型属性
    var status : CQStatus? {
        didSet{
            //把模型传给topView
            topView.status = status
            //把模型传给图片视图
            pictureView.status = status
            //获得根据图片数量得到的不同的宽高 来设置图片视图 宽度/高度约束
            let pictureSize = pictureView.calculateSize()
//            print("宽：\(pictureSize.width)")
//            print("高:\(pictureSize.height)")
            pictureConsWidth?.constant = pictureSize.width
            pictureConsHeight?.constant = pictureSize.height

            
            
            //给内容label设置文字
            contentLabel.text = status?.text
         
        }
    }
    
    //MARK: - 重写布局行高
    func rowOfHeight(status: CQStatus) ->CGFloat {
        //设置cell的模型 这样会再次执行 var status : CQStatus?的属性 再次给topView picture赋值
        self.status = status
        //然后再刷新新的数据 就可以计算出当前的行高了
        layoutIfNeeded()
        //获得bottomView的最大Y作为整个cell的高度 返回
        let height = CGRectGetMaxY(bottomView.frame)
        return height
        
    }
    
    //MARK: - 重写cell
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        //准备UI
        prepareUI()
    }
   
    //MARK: - 准备UI
    func prepareUI() {
      //添加子控件
        
        contentView.addSubview(topView)
        contentView.addSubview(contentLabel)
        contentView.addSubview(pictureView)
        contentView.addSubview(bottomView)
        
        
     //添加topView的约束
        topView.ff_AlignInner(type: ff_AlignType.TopLeft, referView: contentView, size: CGSize(width: UIScreen.screenWidth(), height: 53))
        
        
    //contentLabel的约束
        contentLabel.ff_AlignVertical(type: ff_AlignType.BottomLeft, referView: topView, size: nil, offset: CGPointMake(CQMargin, CQMargin))
        //宽
        contentView.addConstraint(NSLayoutConstraint(item: contentLabel, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: UIScreen.mainScreen().bounds.width-16))
        

        
        
        //底部view的约束
        bottomView.ff_AlignVertical(type: ff_AlignType.BottomLeft, referView: pictureView, size: CGSizeMake(UIScreen.screenWidth(), 44), offset: CGPointMake(-CQMargin, CQMargin))
        
        
    
    }
    
    
    //MARK: - 懒加载
    
   
    //顶部的view
     lazy var topView: CQTopView = CQTopView()
    
    //内容label
     lazy var contentLabel: UILabel = {
        
        let label = UILabel()
        
        label.font = UIFont.systemFontOfSize(17)
        label.textColor = UIColor.blackColor()
        //换行
        label.numberOfLines = 0
        
        
        return label
        
        }()
    
    //图片视图
     lazy var pictureView: CQPictureView = CQPictureView()
    
    
    //底部的view
     lazy var bottomView: CQBottomView = CQBottomView()
    
    
}
